﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Mvc;
using MySql.Data.MySqlClient;
using DapperExtensions;

namespace AspNetSample.Controllers
{
    public class HomeController : Controller
    {

        public async Task<ActionResult> Index()
        {
            var connectionString = System.IO.File.ReadAllText(Server.MapPath("db.txt"));

            IDbConnection cnn = new MySqlConnection(connectionString);

            cnn.Open();

            var result = new StringBuilder();

            var host = await cnn.QueryFirstAsync("select @@hostname as host");

            result.AppendLine(host.host); // db-slave1

            await cnn.ExecuteAsync("insert into t1(N) values(1)"); // 写入

            host = await cnn.QueryFirstAsync("select @@hostname as host");

            result.AppendLine(host.host); // db-master

            return Content(result.ToString());
        }
    }
}
